#include "all.h"
RCC_ClocksType RCC_ClockFreq;
uint8_t Boot_Up;
void GetUid(uint8_t* pUid)
{
	  uint32_t uid=0;
		uint32_t chipId[3]={0};
		uint8_t  Id[15]={0};
		uint8_t i=0;
		chipId[0]=*(volatile unsigned long *)0x1FFFF7F0;	
		chipId[1]=*(volatile unsigned long *)0x1FFFF7F4;	
		chipId[2]=*(volatile unsigned long *)0x1FFFF7F8;		

		Id[0]=(uint8_t)(chipId[0]&0xFF);
		Id[1]=(uint8_t)((chipId[0]&0xFF00)>>8);	
		Id[2]=(uint8_t)((chipId[0]&0xFF0000)>>16);	
		Id[3]=(uint8_t)((chipId[0]&0xFF000000)>>24);		

		Id[4]=(uint8_t)(chipId[1]&0xFF);
		Id[5]=(uint8_t)((chipId[1]&0xFF00)>>8);	
		Id[6]=(uint8_t)((chipId[1]&0xFF0000)>>16);	
		Id[7]=(uint8_t)((chipId[1]&0xFF000000)>>24);		

		Id[8]=(uint8_t)(chipId[2]&0xFF);
		Id[9]=(uint8_t)((chipId[2]&0xFF00)>>8);	
		Id[10]=(uint8_t)((chipId[2]&0xFF0000)>>16);	
		Id[11]=(uint8_t)((chipId[2]&0xFF000000)>>24);	
		
		uid = (chipId[0]>>1)+(chipId[1]>>2)+(chipId[2]>>3);
		for(i=0;i<12;i++)
		{
			Id[12] += Id[i];
		}
		for(i=0;i<12;i++)
		{
			Id[13] ^= Id[i];
		}
		
		pUid[0] = (uint8_t)(uid &0xFF);
		pUid[1] = (uint8_t)((uid &0xFF00)>>8); 
		pUid[2] = (uint8_t)((uid &0xFF0000)>>16); 
		pUid[3] = (uint8_t)((uid &0xFF000000)>>24); 
		pUid[4] = Id[12];
		pUid[5] = Id[13];
		pUid[6] = Bed_Code;		
}
void BSP_Init(void)
{
	SystemInit();	
	RCC_GetClocksFreqValue(&RCC_ClockFreq); 
	TIM2_Init();
	SPI_NRF_Init();       //NRF24L01*SPI接口初始化// 
/*******************Key****************/	
	key_init();
/*******************ADC****************/		
	ADC_init();
/*******************CD****************/		
	Charge_GPIO_init();
	LED_Init();
}	
void Var_Init(void)
{ 
	NRF24L01_Variable_Init();
	KEYs_Init();	
}	 
int main(void)
{ 

	BSP_Init();
	Var_Init();	
//	Bed_info.head = 0x10;
//	Write_Flash_Buff(Bed_ADDR, 0, (uint8_t *)&Bed_info.head, sizeof(struct tBed_info));//写入遥控器ID FLASH 	
	ReadFlashNBtye(Bed_ADDR,0,(uint8_t *)&Bed_info.head,sizeof(struct tBed_info));//读取床垫信息	
	GetUid(Bed_info.ID);
	while (1)
	{
		if(Boot_Up==0)
		{
			LED1_OFF();
			LED2_OFF();			
			LED_Time.Flag = 0;
			RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, DISABLE);//使能低速总线时钟
			TIM_Enable(TIM2, DISABLE);
			TIM_ConfigInt(TIM2,TIM_INT_UPDATE,DISABLE);	
			RCC_EnableAPB1PeriphClk( RCC_APB1_PERIPH_TIM3  , DISABLE);	
			RCC_EnableAPB2PeriphClk(  RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, DISABLE);			
			TIM_Enable(TIM3, DISABLE);				
			Key_GPIO_init();
			GPIO_ResetBits(Key_Column_GPIO,GPIO_PIN_1);
			GPIO_ResetBits(Key_Column_GPIO,GPIO_PIN_2);
			GPIO_ResetBits(Key_Column_GPIO,GPIO_PIN_3);
			GPIO_ResetBits(Key_Column_GPIO,GPIO_PIN_4);
			NRF_PowerDown_Mode();
			Adc_Dis();	
			PWR_EnterStopState(PWR_REGULATOR_LOWPOWER, PWR_STOPENTRY_WFI);	//进入WFE睡眠模式,仅事件能唤醒
			BSP_Init();
			NRF24L01_Variable_Init();
			Auto_Down_init();
			KEYs_Init();
			Boot_Up=1;	
		}	
		else	
		{
			ADC_APP();			
			Key_Task();
			NRF24L01_Handle();		
			Auto_Down_Task();		
			LED_Task();				
		}			
	}
}


/**
 * @brief Assert failed function by user.
 * @param file The name of the call that failed.
 * @param line The source line number of the call that failed.
 */
#ifdef USE_FULL_ASSERT
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
    while (1)
    {

    }
}
#endif // USE_FULL_ASSERT



